// ItemTab.cpp : implementation file
//
#ifndef OBLIVION

#include "stdafx.h"
#include "ItemTab.h"
#include "afxdialogex.h"

#include "AffixFilter.h"
#include "AffixFilterDlg.h"
#include "UtilitiesMFC.h"

// CItemTab dialog

IMPLEMENT_DYNAMIC(CItemTab, CDialogEx)

CItemTab::CItemTab(CWnd* pParent /*=NULL*/)
	: CFilterTab(CItemTab::IDD, pParent)
	, nameFilter(_T(""))
	, app(FALSE)
	, arm(FALSE)
	, wep(FALSE)
	, CIgnNWRes(IDB_CUSTOM_CHECKS, 13)
	, CHideRings(IDB_CUSTOM_CHECKS, 13)
	, CHideAmulet(IDB_CUSTOM_CHECKS, 13)
	, CBipedHead(IDB_CUSTOM_CHECKS, 13)
	, CBipedHair(IDB_CUSTOM_CHECKS, 13)
	, CBipedBodyUp(IDB_CUSTOM_CHECKS, 13)
	, CBipedBodyLo(IDB_CUSTOM_CHECKS, 13)
	, CBipedHand(IDB_CUSTOM_CHECKS, 13)
	, CBipedFoot(IDB_CUSTOM_CHECKS, 13)
	, CBipedRingR(IDB_CUSTOM_CHECKS, 13)
	, CBipedRingL(IDB_CUSTOM_CHECKS, 13)
	, CBipedAmulet(IDB_CUSTOM_CHECKS, 13)
	, CBipedWeapon(IDB_CUSTOM_CHECKS, 13)
	, CBipedWeaponBack(IDB_CUSTOM_CHECKS, 13)
	, CBipedWeaponSide(IDB_CUSTOM_CHECKS, 13)
	, CBipedQuiver(IDB_CUSTOM_CHECKS, 13)
	, CBipedShield(IDB_CUSTOM_CHECKS, 13)
	, CBipedTorch(IDB_CUSTOM_CHECKS, 13)
	, CBipedTail(IDB_CUSTOM_CHECKS, 13)
{

}

CItemTab::~CItemTab()
{

}

void CItemTab::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_NAME_FILTER, CNameFilter);
	DDX_Control(pDX, IDC_VAL_MIN, CMinVal);
	DDX_Control(pDX, IDC_VAL_MAX, CMaxVal);
	DDX_Control(pDX, IDC_WHT_MIN, CMinWht);
	DDX_Control(pDX, IDC_WHT_MAX, CMaxWht);
	DDX_Control(pDX, IDC_DUR_MIN, CMinDur);
	DDX_Control(pDX, IDC_DUR_MAX, CMaxDur);
	DDX_Control(pDX, IDC_DMG_MIN, CMinDmg);
	DDX_Control(pDX, IDC_DMG_MAX, CMaxDmg);
	DDX_Control(pDX, IDC_RNG_MIN, CMinRng);
	DDX_Control(pDX, IDC_RNG_MAX, CMaxRng);
	DDX_Control(pDX, IDC_SPD_MIN, CMinSpd);
	DDX_Control(pDX, IDC_SPD_MAX, CMaxSpd);
	DDX_Control(pDX, IDC_AR_MIN, CMinAR);
	DDX_Control(pDX, IDC_AR_MAX, CMaxAR);
	DDX_Control(pDX, IDC_WEP_TYPES, CWepTypes);
	DDX_Control(pDX, IDC_APP_TYPES, CAppTypes);
	DDX_Control(pDX, IDC_IGNORES_NW_RES, CIgnNWRes);
	DDX_Control(pDX, IDC_MODEL_FILTER_WEP, CModelFilterWeapon);
	DDX_Control(pDX, IDC_ICON_FILTER_WEP, CIconFilterWeapon);
	DDX_Control(pDX, IDC_HIDE_RINGS, CHideRings);
	DDX_Control(pDX, IDC_HIDE_AMULET, CHideAmulet);
	DDX_Control(pDX, IDC_MODEL_FILTER_BIPED_MALE, CModelFilterBipedMale);
	DDX_Control(pDX, IDC_MODEL_FILTER_BIPED_FEMALE, CModelFilterBipedFemale);
	DDX_Control(pDX, IDC_MODEL_FILTER_GROUND_MALE, CModelFilterGroundMale);
	DDX_Control(pDX, IDC_MODEL_FILTER_GROUND_FEMALE, CModelFilterGroundFemale);
	DDX_Control(pDX, IDC_ICON_FILTER_APP_MALE, CIconFilterMale);
	DDX_Control(pDX, IDC_ICON_FILTER_APP_FEMALE, CIconFilterFemale);
	DDX_Control(pDX, IDC_HEAD, CBipedHead);
	DDX_Control(pDX, IDC_HAIR, CBipedHair);
	DDX_Control(pDX, IDC_BODY_UP, CBipedBodyUp);
	DDX_Control(pDX, IDC_BODY_LO, CBipedBodyLo);
	DDX_Control(pDX, IDC_HAND, CBipedHand);
	DDX_Control(pDX, IDC_FOOT, CBipedFoot);
	DDX_Control(pDX, IDC_RING_RIGHT, CBipedRingR);
	DDX_Control(pDX, IDC_RING_LEFT, CBipedRingL);
	DDX_Control(pDX, IDC_AMULET, CBipedAmulet);
	DDX_Control(pDX, IDC_WEAPON, CBipedWeapon);
	DDX_Control(pDX, IDC_WEAPON_BACK, CBipedWeaponBack);
	DDX_Control(pDX, IDC_WEAPON_SIDE, CBipedWeaponSide);
	DDX_Control(pDX, IDC_QUIVER, CBipedQuiver);
	DDX_Control(pDX, IDC_SHIELD, CBipedShield);
	DDX_Control(pDX, IDC_TORCH, CBipedTorch);
	DDX_Control(pDX, IDC_TAIL, CBipedTail);
}


BEGIN_MESSAGE_MAP(CItemTab, CDialogEx)
	ON_LBN_SELCHANGE(IDC_APP_TYPES, &CItemTab::OnLbnSelchangeAppTypes)
	ON_LBN_SELCHANGE(IDC_WEP_TYPES, &CItemTab::OnLbnSelchangeWepTypes)
	ON_EN_CHANGE(IDC_NAME_FILTER, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_VAL_MIN, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_VAL_MAX, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_WHT_MIN, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_WHT_MAX, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_DUR_MIN, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_DUR_MAX, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_AR_MIN, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_AR_MAX, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_MODEL_FILTER_BIPED_MALE, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_MODEL_FILTER_BIPED_FEMALE, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_MODEL_FILTER_GROUND_MALE, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_MODEL_FILTER_GROUND_FEMALE, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_ICON_FILTER_APP_MALE, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_ICON_FILTER_APP_FEMALE, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_DMG_MIN, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_DMG_MAX, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_RNG_MIN, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_RNG_MAX, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_SPD_MIN, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_SPD_MAX, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_MODEL_FILTER_WEP, &CItemTab::OnModify)
	ON_EN_CHANGE(IDC_ICON_FILTER_WEP, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_HIDE_RINGS, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_HIDE_AMULET, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_HEAD, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_HAIR, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_BODY_UP, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_BODY_LO, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_HAND, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_FOOT, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_RING_RIGHT, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_RING_LEFT, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_AMULET, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_WEAPON, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_WEAPON_BACK, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_WEAPON_SIDE, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_QUIVER, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_SHIELD, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_TORCH, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_TAIL, &CItemTab::OnModify)
	ON_BN_CLICKED(IDC_IGNORES_NW_RES, &CItemTab::OnModify)
END_MESSAGE_MAP()


bool CItemTab::New()
{
	// initialize type selection
	app = FALSE;
	arm = FALSE;
	wep = FALSE;
	CAppTypes.SetSel(-1, FALSE);
	CWepTypes.SetSel(-1, FALSE);

	// initialize the tristate boxes to indeterminate
	CButton* tri_fields[] = {
		&CHideRings, &CHideAmulet,
		&CBipedHead, &CBipedHair, &CBipedBodyUp, &CBipedBodyLo, &CBipedHand, &CBipedFoot,
		&CBipedRingR, &CBipedRingL, &CBipedAmulet,
		&CBipedWeapon, &CBipedWeaponBack, &CBipedWeaponSide, &CBipedQuiver,
		&CBipedShield, &CBipedTorch, &CBipedTail,
		&CIgnNWRes
	};
	for (int i = 0; i < lengthof(tri_fields); ++i) tri_fields[i]->SetCheck(BST_INDETERMINATE);

	// clear the textboxes
	CEdit* fields[] = {
		&CNameFilter,
		&CMinVal, &CMaxVal,
		&CMinWht, &CMaxWht,
		&CMinDur, &CMaxDur,
		&CMinAR, &CMaxAR,
		&CModelFilterBipedMale, &CModelFilterBipedFemale,
		&CModelFilterGroundMale, &CModelFilterGroundFemale,
		&CIconFilterMale, &CIconFilterFemale,
		&CMinDmg, &CMaxDmg,
		&CMinRng, &CMaxRng,
		&CMinSpd, &CMaxSpd,
		&CModelFilterWeapon, &CIconFilterWeapon
	};
	for (int i = 0; i < lengthof(fields); ++i) fields[i]->SetWindowText("");
	
	CWnd* apparel_fields[] = {
		&CHideRings, &CHideAmulet,
		&CBipedHead, &CBipedHair, &CBipedBodyUp, &CBipedBodyLo, &CBipedHand, &CBipedFoot,
		&CBipedRingR, &CBipedRingL, &CBipedAmulet,
		&CBipedWeapon, &CBipedWeaponBack, &CBipedWeaponSide, &CBipedQuiver,
		&CBipedShield, &CBipedTorch, &CBipedTail,
		&CModelFilterBipedMale, &CModelFilterGroundMale, &CIconFilterMale,
		&CModelFilterBipedFemale, &CModelFilterGroundFemale, &CIconFilterFemale
	};
	for (int i = 0; i < lengthof(apparel_fields); ++i) apparel_fields[i]->EnableWindow(app);
	
	CWnd* armor_fields[] = {
		&CMinAR, &CMaxAR
	};
	for (int i = 0; i < lengthof(armor_fields); ++i) armor_fields[i]->EnableWindow(arm);
	
	CWnd* weapon_fields[] = {
		&CMinDmg, &CMaxDmg, &CMinRng, &CMaxRng, &CMinSpd, &CMaxSpd,
		&CIgnNWRes,
		&CModelFilterWeapon, &CIconFilterWeapon
	};
	for (int i = 0; i < lengthof(weapon_fields); ++i) weapon_fields[i]->EnableWindow(wep);
	
	CWnd* durable_fields[] = {
		&CMinDur, &CMaxDur
	};
	for (int i = 0; i < lengthof(durable_fields); ++i) durable_fields[i]->EnableWindow(arm||wep);

	return true;
}


bool CItemTab::Save(AffixFilter& flt) const
{
	flt.app = app;
	flt.app_types = 0;
	if ( app )
		for ( int i = 0; i < CAppTypes.GetCount(); ++i )
			if ( CAppTypes.GetSel(i) != 0 && CAppTypes.GetSel(i) != LB_ERR )
				flt.app_types |= 1 << i;
	_VMESSAGE("app_types = %02X", flt.app_types);
	flt.arm = arm;

	flt.wep = wep;
	flt.wep_types = 0;
	if ( wep )
		for ( int i = 0; i < CWepTypes.GetCount(); ++i )
			if ( CWepTypes.GetSel(i) != 0 && CWepTypes.GetSel(i) != LB_ERR )
				flt.wep_types |= 1 << i;
	_VMESSAGE("wep_types = %02X", flt.wep_types);

	typedef bool (AffixFilter::*numSetFn)(double val);
	numSetFn nums[] = {
		&AffixFilter::SetMinValue, &AffixFilter::SetMaxValue,
		&AffixFilter::SetMinWeight, &AffixFilter::SetMaxWeight,
		&AffixFilter::SetMinDurability, &AffixFilter::SetMaxDurability,
		&AffixFilter::SetMinArmorRating, &AffixFilter::SetMaxArmorRating,
		&AffixFilter::SetMinDamage, &AffixFilter::SetMaxDamage,
		&AffixFilter::SetMinReach, &AffixFilter::SetMaxReach,
		&AffixFilter::SetMinSpeed, &AffixFilter::SetMaxSpeed
	};
	const CEdit* num_fields[] = {
		&CMinVal, &CMaxVal,
		&CMinWht, &CMaxWht,
		&CMinDur, &CMaxDur,
		&CMinAR, &CMaxAR,
		&CMinDmg, &CMaxDmg,
		&CMinRng, &CMaxRng,
		&CMinSpd, &CMaxSpd
	};
	BOOST_STATIC_ASSERT_MSG(lengthof(nums) == lengthof(num_fields), "nums and num_fields do not have the same size.");
	for ( UInt8 i = 0; i < lengthof(nums); ++i ) {
		(flt.*nums[i])(CEditToDbl(*num_fields[i]));
		_VMESSAGE("Num field #%i set to %0.2f", i, CEditToDbl(*num_fields[i]));
	}

	typedef bool (AffixFilter::*triSetFn)(tri check);
	triSetFn tris[] = {
		&AffixFilter::SetHidesRings, &AffixFilter::SetHidesAmulet,
		&AffixFilter::SetBipedHead, &AffixFilter::SetBipedHair, &AffixFilter::SetBipedBodyUpper, &AffixFilter::SetBipedBodyLower, &AffixFilter::SetBipedHand, &AffixFilter::SetBipedFoot,
		&AffixFilter::SetBipedRingRight, &AffixFilter::SetBipedRingLeft, &AffixFilter::SetBipedAmulet,
		&AffixFilter::SetBipedWeapon, &AffixFilter::SetBipedWeaponBack, &AffixFilter::SetBipedWeaponSide, &AffixFilter::SetBipedQuiver,
		&AffixFilter::SetBipedShield, &AffixFilter::SetBipedTorch, &AffixFilter::SetBipedTail,
		&AffixFilter::SetIgnoresNormalWeaponResistance
	};
	const CButton* checks[] = {
		&CHideRings, &CHideAmulet,
		&CBipedHead, &CBipedHair, &CBipedBodyUp, &CBipedBodyLo, &CBipedHand, &CBipedFoot,
		&CBipedRingR, &CBipedRingL, &CBipedAmulet,
		&CBipedWeapon, &CBipedWeaponBack, &CBipedWeaponSide, &CBipedQuiver,
		&CBipedShield, &CBipedTorch, &CBipedTail,
		&CIgnNWRes
	};
	BOOST_STATIC_ASSERT_MSG(lengthof(tris) == lengthof(checks), "tris and checks do not have the same size.");
	for ( UInt8 i = 0; i < lengthof(tris); ++i )
		(flt.*tris[i])(checks[i]->GetCheck());
	
	CString buf = CString();
	typedef bool (AffixFilter::*textSetFn)(const BSStringT& new_text);
	textSetFn filters[] = {
		&AffixFilter::SetNameFilter,
		&AffixFilter::SetModelFilterBipedMale, &AffixFilter::SetModelFilterBipedFemale,
		&AffixFilter::SetModelFilterGroundMale, &AffixFilter::SetModelFilterGroundFemale,
		&AffixFilter::SetIconFilterMale, &AffixFilter::SetIconFilterFemale,
		&AffixFilter::SetModelFilterWeapon, &AffixFilter::SetIconFilterWeapon
	};
	const CEdit* text_fields[] = {
		&CNameFilter,
		&CModelFilterBipedMale, &CModelFilterBipedFemale,
		&CModelFilterGroundMale, &CModelFilterGroundFemale,
		&CIconFilterMale, &CIconFilterFemale,
		&CModelFilterWeapon, &CIconFilterWeapon
	};
	BOOST_STATIC_ASSERT_MSG(lengthof(filters) == lengthof(text_fields), "filters and text_fields do not have the same size.");
	for ( UInt8 i = 0; i < lengthof(filters); ++i ) {
		text_fields[i]->GetWindowText(buf);
		(flt.*filters[i])((const char*)buf);
	}

	return true;
}


bool CItemTab::Load(const AffixFilter& flt)
{
	app = flt.app;
	if ( app ) {
		for ( int i = 0; i < CAppTypes.GetCount(); ++i ) {
			BOOL uses = (flt.app_types & (1 << i)) != 0;
			CAppTypes.SetSel(i, uses);
//			_DMESSAGE("%s app type #%i.", (uses?"Selected":"Unselected"), i);
		}
	}
	else
		CAppTypes.SelItemRange(FALSE, 0, CAppTypes.GetCount()-1);

	arm = flt.arm;

	wep = flt.wep;
	if ( wep ) {
		for ( int i = 0; i < CWepTypes.GetCount(); ++i ) {
			BOOL uses = (flt.wep_types & (1 << i)) != 0;
			CWepTypes.SetSel(i, uses);
//			_DMESSAGE("%s wep type #%i.", (uses?"Selected":"Unselected"), i);
		}
	}
	else
		CWepTypes.SelItemRange(FALSE, 0, CWepTypes.GetCount()-1);
	
	CWnd* apparel_fields[] = {
		&CHideRings, &CHideAmulet,
		&CBipedHead, &CBipedHair, &CBipedBodyUp, &CBipedBodyLo, &CBipedHand, &CBipedFoot,
		&CBipedRingR, &CBipedRingL, &CBipedAmulet,
		&CBipedWeapon, &CBipedWeaponBack, &CBipedWeaponSide, &CBipedQuiver,
		&CBipedShield, &CBipedTorch, &CBipedTail,
		&CModelFilterBipedMale, &CModelFilterGroundMale, &CIconFilterMale,
		&CModelFilterBipedFemale, &CModelFilterGroundFemale, &CIconFilterFemale
	};
	for (int i = 0; i < lengthof(apparel_fields); ++i) apparel_fields[i]->EnableWindow(app);
	
	CWnd* armor_fields[] = {
		&CMinAR, &CMaxAR
	};
	for (int i = 0; i < lengthof(armor_fields); ++i) armor_fields[i]->EnableWindow(arm);
	
	CWnd* weapon_fields[] = {
		&CMinDmg, &CMaxDmg, &CMinRng, &CMaxRng, &CMinSpd, &CMaxSpd,
		&CIgnNWRes,
		&CModelFilterWeapon, &CIconFilterWeapon
	};
	for (int i = 0; i < lengthof(weapon_fields); ++i) weapon_fields[i]->EnableWindow(wep);
	
	CWnd* durable_fields[] = {
		&CMinDur, &CMaxDur
	};
	for (int i = 0; i < lengthof(durable_fields); ++i) durable_fields[i]->EnableWindow(arm||wep);

	typedef double (AffixFilter::* numGetFn)(void) const;
	numGetFn nums[] = {
		&AffixFilter::GetMinValue, &AffixFilter::GetMaxValue,
		&AffixFilter::GetMinWeight, &AffixFilter::GetMaxWeight,
		&AffixFilter::GetMinDurability, &AffixFilter::GetMaxDurability,
		&AffixFilter::GetMinArmorRating, &AffixFilter::GetMaxArmorRating,
		&AffixFilter::GetMinDamage, &AffixFilter::GetMaxDamage,
		&AffixFilter::GetMinReach, &AffixFilter::GetMaxReach,
		&AffixFilter::GetMinSpeed, &AffixFilter::GetMaxSpeed
	};
	CEdit* num_fields[] = {
		&CMinVal, &CMaxVal,
		&CMinWht, &CMaxWht,
		&CMinDur, &CMaxDur,
		&CMinAR, &CMaxAR,
		&CMinDmg, &CMaxDmg,
		&CMinRng, &CMaxRng,
		&CMinSpd, &CMaxSpd
	};
	BOOST_STATIC_ASSERT_MSG(lengthof(nums) == lengthof(num_fields), "nums and num_fields do not have the same size.");
	for ( UInt8 i = 0; i < lengthof(nums); ++i )
		DblToCEdit(*num_fields[i], (flt.*nums[i])());

	typedef tri (AffixFilter::* triGetFn)(void) const;
	triGetFn tris[] = {
		&AffixFilter::GetHidesRings, &AffixFilter::GetHidesAmulet,
		&AffixFilter::GetBipedHead, &AffixFilter::GetBipedHair, &AffixFilter::GetBipedBodyUpper, &AffixFilter::GetBipedBodyLower, &AffixFilter::GetBipedHand, &AffixFilter::GetBipedFoot,
		&AffixFilter::GetBipedRingRight, &AffixFilter::GetBipedRingLeft, &AffixFilter::GetBipedAmulet,
		&AffixFilter::GetBipedWeapon, &AffixFilter::GetBipedWeaponBack, &AffixFilter::GetBipedWeaponSide, &AffixFilter::GetBipedQuiver,
		&AffixFilter::GetBipedShield, &AffixFilter::GetBipedTorch, &AffixFilter::GetBipedTail,
		&AffixFilter::GetIgnoresNormalWeaponResistance
	};
	CButton* checks[] = {
		&CHideRings, &CHideAmulet,
		&CBipedHead, &CBipedHair, &CBipedBodyUp, &CBipedBodyLo, &CBipedHand, &CBipedFoot,
		&CBipedRingR, &CBipedRingL, &CBipedAmulet,
		&CBipedWeapon, &CBipedWeaponBack, &CBipedWeaponSide, &CBipedQuiver,
		&CBipedShield, &CBipedTorch, &CBipedTail,
		&CIgnNWRes
	};
	BOOST_STATIC_ASSERT_MSG(lengthof(tris) == lengthof(checks), "tris and checks do not have the same size.");
	for ( UInt8 i = 0; i < lengthof(tris); ++i )
		checks[i]->SetCheck((flt.*tris[i])());
	
	CString buf = CString();
	typedef BSStringT (AffixFilter::* textGetFn)(void) const;
	textGetFn filters[] = {
		&AffixFilter::GetNameFilter,
		&AffixFilter::GetModelFilterBipedMale, &AffixFilter::GetModelFilterBipedFemale,
		&AffixFilter::GetModelFilterGroundMale, &AffixFilter::GetModelFilterGroundFemale,
		&AffixFilter::GetIconFilterMale, &AffixFilter::GetIconFilterFemale,
		&AffixFilter::GetModelFilterWeapon, &AffixFilter::GetIconFilterWeapon
	};
	CEdit* text_fields[] = {
		&CNameFilter,
		&CModelFilterBipedMale, &CModelFilterBipedFemale,
		&CModelFilterGroundMale, &CModelFilterGroundFemale,
		&CIconFilterMale, &CIconFilterFemale,
		&CModelFilterWeapon, &CIconFilterWeapon
	};
	BOOST_STATIC_ASSERT_MSG(lengthof(filters) == lengthof(text_fields), "filters and text_fields do not have the same size.");
	for ( UInt8 i = 0; i < lengthof(filters); ++i ) {
		buf = (flt.*filters[i])().c_str();
		text_fields[i]->SetWindowText(buf);
	}

	return true;
}


// CItemTab message handlers


BOOL CItemTab::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// apparel types list
	CAppTypes.AddString("Clothing");
	CAppTypes.AddString("Light Armor");
	CAppTypes.AddString("Heavy Armor");

	// weapon types list
	CWepTypes.AddString("Blade One Hand");
	CWepTypes.AddString("Blade Two Hand");
	CWepTypes.AddString("Blunt One Hand");
	CWepTypes.AddString("Blunt Two Hand");
	CWepTypes.AddString("Bow");
	CWepTypes.AddString("Ammo");
	CWepTypes.AddString("Staff");

	New();

	return FALSE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CItemTab::OnLbnSelchangeAppTypes()
{
	app = CAppTypes.GetSelCount()>0;
	CWnd* apparel_fields[] = {
		&CHideRings, &CHideAmulet,
		&CBipedHead, &CBipedHair, &CBipedBodyUp, &CBipedBodyLo, &CBipedHand, &CBipedFoot,
		&CBipedRingR, &CBipedRingL, &CBipedAmulet,
		&CBipedWeapon, &CBipedWeaponBack, &CBipedWeaponSide, &CBipedQuiver,
		&CBipedShield, &CBipedTorch, &CBipedTail,
		&CModelFilterBipedMale, &CModelFilterGroundMale, &CIconFilterMale,
		&CModelFilterBipedFemale, &CModelFilterGroundFemale, &CIconFilterFemale
	};
	for (int i = 0; i < lengthof(apparel_fields); ++i) apparel_fields[i]->EnableWindow(app);

	arm = CAppTypes.GetSel(1) || CAppTypes.GetSel(2);
	CWnd* armor_fields[] = {
		&CMinAR, &CMaxAR
	};
	for (int i = 0; i < lengthof(armor_fields); ++i) armor_fields[i]->EnableWindow(arm);

	CWnd* durable_fields[] = {
		&CMinDur, &CMaxDur
	};
	for (int i = 0; i < lengthof(durable_fields); ++i) durable_fields[i]->EnableWindow(arm||wep);

	OnModify();
}


void CItemTab::OnLbnSelchangeWepTypes()
{
	wep = CWepTypes.GetSelCount()>0;
	CWnd* weapon_fields[] = {
		&CMinDmg, &CMaxDmg, &CMinRng, &CMaxRng, &CMinSpd, &CMaxSpd,
		&CIgnNWRes,
		&CModelFilterWeapon, &CIconFilterWeapon
	};
	for (int i = 0; i < lengthof(weapon_fields); ++i) weapon_fields[i]->EnableWindow(wep);

	CWnd* durable_fields[] = {
		&CMinDur, &CMaxDur
	};
	for (int i = 0; i < lengthof(durable_fields); ++i) durable_fields[i]->EnableWindow(arm||wep);

	OnModify();
}


void CItemTab::OnModify()
{
	if ( parent )
		parent->dlgMod(true);
}

#endif
